#pragma once

#include "log.hpp"
#include <cstring>
#include <cerrno>
#include <sys/epoll.h>

namespace Epoll
{
    static const int default_size = 1024;
    
    // 创建Epoll模型函数
    static int EpollCreate(int size = default_size)
    {
        int epfd = epoll_create(size);
        if(epfd < 0)
            logMessage(FATAL, "Epoll create fail, %d:%s\n", errno, strerror(errno));
        else
            logMessage(NORMAL, "Epoll create success, epfd:%d\n", epfd);
        return epfd;
    }

    // 进行Epoll控制函数
    static int EpollCtl(int epfd, int op, int fd, uint32_t events)
    {
        struct epoll_event event;
        event.events = EPOLLIN;
        event.data.fd = fd;

        int ret = epoll_ctl(epfd, op, fd, &event);
        if(ret < 0) logMessage(ERROR, "Epoll control fail, %d:%s\n", errno, strerror(errno));
        else logMessage(NORMAL, "Epoll control success, ret:%d\n", ret);

        return ret;
    }

    // Epoll等待函数
    static int EpollWait(int epfd, struct epoll_event* events, int maxevents, int timeout)
    {
        return epoll_wait(epfd, events, maxevents, timeout);
    }
}